//
//  CartoonHorizontalTableViewCell.m
//  WuLi
//
//  Created by Gollum on 16/9/23.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "CartoonHorizontalTableViewCell.h"
#import "CartoonHorizontalCollectionViewCell.h"
#import "NetworkTool.h"
#import "UIImageView+WebCache.h"

#import "CartoonDetailData.h"
#import "CartoonData.h"
#import "CartoonModel.h"

#define kCartoonHorizontalCollectionViewCellReuse @"CartoonHorizontalReuse"
@interface CartoonHorizontalTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)CartoonModel *cartoonModel;

@end



@implementation CartoonHorizontalTableViewCell

- (void)awakeFromNib {
    // Initialization code
   _dataArray = [NSMutableArray array];
    [self handleData];
    //布局
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //根据自己的需求设置宽高
    [flowLayout setItemSize:CGSizeMake(self.backView.frame.size.width / 3 - 100 , 30)];
    //竖直滑动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //内边距，列、行间距
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    [self.collectionView setCollectionViewLayout:flowLayout];
    //注册（xib）
    
    
    UINib *nib = [UINib nibWithNibName:@"CartoonHorizontalCollectionViewCell" bundle:[NSBundle mainBundle]];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    _collectionView.scrollEnabled = NO;
    _collectionView.userInteractionEnabled = YES;
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonHorizontalCollectionViewCellReuse];
    
}
- (void)handleData{

    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=1&start=0";
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _cartoonModel = [CartoonModel modelWithDic:result];
        [_dataArray addObject:_cartoonModel];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _cartoonModel.data.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonHorizontalCollectionViewCellReuse forIndexPath:indexPath];
    
    NSURL *url = [NSURL URLWithString:_cartoonModel.data.data[indexPath.row].thumb_1];
    [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
    cell.titleLabel.text = _cartoonModel.data.data[indexPath.row].title;
    
    return cell;

}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.backView.frame.size.width / 2, [UIScreen mainScreen].bounds.size.width / 2 * 0.8 );
}






- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}





@end
